<?php
//error_reporting(E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR);
require('./roots.php');
require($root_path.'include/inc_environment_global.php');

/**
* CARE2X Integrated Hospital Information System beta 2.0.1 - 2004-07-04
* GNU General Public License
* Copyright 2002,2003,2004,2005,2006 Elpidio Latorilla
* elpidio@care2x.org, 
*
* See the file "copy_notice.txt" for the licence notice
*/

$lang_tables[]='interconsulta.php';
define('LANG_FILE','history.php');
define('NO_2LEVEL_CHK',1);
require_once($root_path.'include/inc_front_chain_lang.php');

$update = $_GET['update'];

if (!isset($update))
{
    $update=0;
}

$thisfile=basename(__FILE__);
$breakfile=$root_path.'modules/registration_admission/registration_admission.php';
$action='historia_clinica-save.php'.URL_APPEND . '&update='.$update;

require_once($root_path.'include/care_api_classes/class_core.php');
$core=new Core;

require_once($root_path.'include/care_api_classes/class_gui_history.php');
require_once($root_path.'include/care_api_classes/class_interconsulta.php');

$pid=$_GET['pid'];
$id = $_GET['id'];
$minimal = $_GET['minimal'];
$ficha = $_GET['ficha'];

if( ($pid==NULL)&&($ficha!=NULL)){
	header ("Location: historia_clinica.php?sid=$sid&lang=$lang"); 
} 

$guihistory = new GuiHistory($ficha);
$campos = $guihistory->getAllSoapInfoObject($pid, $id);
$files = $guihistory->seleccionarNombreAdjuntos($id);

if (!isset($id))
{
    $id = -2;
}
include_once('../../include/inc_date_format_functions.php');
require_once ('../../js/jscalendar/calendar.php');
$calendar = new DHTML_Calendar('../../js/jscalendar/', $lang, 'calendar-system', true);
$calendar->load_files();

# Start Smarty templating here
/**
* LOAD Smarty
*/
# Note: it is advisable to load this after the inc_front_chain_lang.php so
# that the smarty script can use the user configured template theme

require_once($root_path.'gui/smarty_template/smarty_care.class.php');
$smarty = new smarty_care('common');


//Llamada a la funcion de consulta en la capa de db
//Importacion y creacion de instancia de la clase queries_historia_clinica_soap.php
$filename_historia = $root_path."db/historia_clinica/queries_historia_clinica_soap.php";
require_once($filename_historia);
$query_historia = new Queries_historia_clinica_soap();

$auditor = false;
//$sql='SELECT name, login_id, password, permission, lockflag FROM care_users WHERE login_id=\''.addslashes($_SESSION['sess_login_userid']).'\'';
$sql= $query_historia->historia_clinica_soap_consulta($_SESSION['sess_login_userid']);

// buscado array ('login_id'=>\'''.addslashes($_SESSION['sess_login_userid']')
// valores nuevos array (''=>,)

//echo $sql;
if($ergebnis=$db->SelectUserInfo($_SESSION['sess_login_userid']))
  {
    $zeile=$ergebnis;
    
    if (($zeile['permission'] == '_a_3_historiaauditor')||($zeile['permission'] == 'System_Admin'))
      {
	$disabled = "";
	$auditor = true;
      } 
    else 
      {
	$disabled = "disabled";
	$auditor = false;
      }
    
  }
 
  
if ($campos && $id > 0)
  {
    //ya hay informacion dentro de soap y solo deberia entrar por aqui en consultar
    $smarty->assign('camp',1);
    $i=0;
    $temp=$campos[$i];
    while ($temp!=NULL)
    {
//         $field = "<tr><td>$LDDate</td><td>" . $calendar->show_calendar($calendar,'dd/MM/yyyy', 'fecha',$temp['fecha']) . "</td></tr>";
        $field = '<tr><td>' . $LDDate . '</td><td><input name="fecha" type="text" ' . $disabled . ' value="' . $temp['fecha'] . '"></td></tr>';
        $field .= "<tr><td>".$LDNombreMedico."</td><td><input type='text' ' . $disabled . ' name='nombre_medico' value='" . $temp['nombre_medico']. "'></td></tr>";
        $field .= "<tr><td>".$LDMatriculaMedico."</td><td><input type='text' ' . $disabled . ' name='matricula_medico' value='" . $temp['matricula_medico'] . "'></td></tr>";
        $field .= "<tr><td>".$LDClasificacionMedico."</td><td><input type='text' ' . $disabled . ' name='clasificacion_medico' value='" . $temp['clasificacion_medico'] . "'></td></tr>";
        $field .= "<tr><td>".$LDRazones."</td><td><textarea name='subjetivo' ' . $disabled . ' cols='22'>" . $temp['subjetivo'] . "</textarea></td></tr>";
        $field .= "<tr><td>".$LDObjetivo."</td><td><textarea name='objetivo' ' . $disabled . ' cols='22'>".$temp['objetivo'] . "</textarea></td></tr>";
        $field .= "<tr><td>".$LDDiagnostico."</td><td><textarea name='apreciacion' ' . $disabled . ' cols='22'>" . $temp['apreciacion'] . "</textarea></td></tr>";
        $field .= "<tr><td>".$LDPlan."</td><td><textarea name='planTPR' ' . $disabled . ' cols='22'>". $temp['planTPR'] . "</textarea></td></tr>";
        $field .= "<tr><td>".$LDTratamiento."</td><td><textarea ' . $disabled . ' name='tratamiento' cols='22'>" . $temp['tratamiento'] . "</textarea></td></tr>";

        switch ($temp['tipo'])
        {
            case 'P':
                $field .= "<tr><td>".$LDTipo."</td><td><input type='radio' name='tipo' ' . $disabled . ' value='" . $temp['tipo'] . "' checked>P</td></tr>";
                $field .= "<tr><td>&nbsp;</td><td><input type='radio' name='tipo' ' . $disabled . ' value='S'>S</td></tr>";
                $field .= "<tr><td>&nbsp;</td><td><input type='radio' name='tipo' ' . $disabled . ' value='X'>X</td></tr>";
                break;
            case 'S':
                $field .= "<tr><td>".$LDTipo."</td><td><input type='radio' name='tipo' ' . $disabled . ' value='" . $temp['tipo'] . "' checked>P</td></tr>";
                $field .= "<tr><td>&nbsp;</td><td><input type='radio' name='tipo' ' . $disabled . ' value='P'>P</td></tr>";
                $field .= "<tr><td>&nbsp;</td><td><input type='radio' name='tipo' ' . $disabled . ' value='X'>X</td></tr>";
                break;
            case 'X':
                $field .= "<tr><td>".$LDTipo."</td><td><input type='radio' name='tipo' ' . $disabled . ' value='" . $temp['tipo'] . "' checked>P</td></tr>";
                $field .= "<tr><td>&nbsp;</td><td><input type='radio' name='tipo' ' . $disabled . ' value='S'>S</td></tr>";
                $field .= "<tr><td>&nbsp;</td><td><input type='radio' name='tipo' ' . $disabled . ' value='P'>P</td></tr>";
                break;
        }
        $field .= "<tr><td>".$LDReferido."</td><td><select name='referido' ' . $disabled . '><option value='SI' ";
        if ($temp['referido'] == "SI") $field .= "selected";
        $field .= ">".$LDSi."</option><option value='NO' ";
        if ($temp['referido'] == "NO") $field .= "selected";
        $field .= ">No</option></select></td></tr>";
        $gui = $field;
        $i++;
        $temp=$campos[$i];
    }
}
else if ($id == 0)
{
    $int = new Interconsulta;
    $temp = $int->preloadUserInfo($_SESSION['sess_login_userid']);
    //el id esta seteado a 0, asi que debe ser nuevo
    $field = '<tr><td>' . $LDDate . '</td><td><input name="fecha" type="text" readonly value="' . date("d/m/Y H:i:s") . '"></td></tr>';
    $field .= "<tr><td>".$LDNombreMedico."</td><td><input type='text' name='nombre_medico' value='".$temp['name']."'></td></tr>";
    $field .= "<tr><td>".$LDMatriculaMedico."</td><td><input type='text' name='matricula_medico' value='".$temp['matricula']."'></td></tr>";
    $field .= "<tr><td>".$LDClasificacionMedico."</td><td><input type='text' name='clasificacion_medico' value='".$temp['clasificacion']."'></td></tr>";
    $field .= "<tr><td>".$LDRazones."</td><td><textarea name='subjetivo' cols='22'></textarea></td></tr>";
    $field .= "<tr><td>".$LDObjetivo."</td><td><textarea name='objetivo' cols='22'></textarea></td></tr>";
    $field .= "<tr><td>".$LDDiagnostico."</td><td><textarea name='apreciacion' cols='22'></textarea></td></tr>";
    $field .= "<tr><td>".$LDPlan."</td><td><textarea name='planTPR' cols='22'></textarea></td></tr>";
    $field .= "<tr><td>".$LDTratamiento."</td><td><textarea name='tratamiento' cols='22'></textarea></td></tr>";
    $field .= "<tr><td>".$LDTipo."</td><td><input type='radio' name='tipo' value='P'>P</td></tr>";
    $field .= "<tr><td>&nbsp;</td><td><input type='radio' name='tipo' value='S'>S</td></tr>";
    $field .= "<tr><td>&nbsp;</td><td><input type='radio' name='tipo' value='X'>X</td></tr>";
    $field .= "<tr><td>".$LDReferido."</td><td><select name='referido'><option value='SI'>".$LDSi."</option><option value='NO'>No</option></select></td></tr>";
    $gui = $field;
}
else if ($campos && $id == -2)
{
    //solo fechas
    $i=0;
    $temp=$campos[$i];
    while ($temp!=NULL)
    {
        $gui[] = $temp;
        $i++;
        $temp=$campos[$i];
    }
    $smarty->assign('camp',1);
    $id=-1;
}
else
{
    //no hay nada de nada
    $smarty->assign('sNoDisponible', $LDNoDisp);
    $smarty->assign('camp',0);
    $id=-1;
}

?>
                <script  language="javascript">

                function popWin(){
                        urlholder="./historia_clinica-adjuntar.php<?php echo URL_REDIRECT_APPEND;?>";
                        DSWIN<?php echo $sid ?>=window.open(urlholder,"wblabel<?php echo $sid ?>","menubar=no,width=400,height=150,resizable=yes,scrollbars=yes");
                }

                </script>
<?php

$archivos="";
if ($files)
{
    if (count($files))
    {
        foreach($files as $file){
                $name = $file['name'];
                $option="menubar=no,width=600,height=6000,resizable=yes,scrollbars=yes";

                if (strlen($name))
                {
                        $url="../../modules/historia_clinica/include/showImage.php?id=$id&name=$name ";
                        $archivos .= "<a href=\"javascript: window.open('$url','wblabel','$option'); exit;\">$name</a> ";
                }
        }
    }
}
if($archivos=='') $archivos=$LDNinguno;

$smarty->assign('breakfile',$breakfile);
$smarty->assign('action',$action);
$smarty->assign('pid',$pid);
$smarty->assign('id',$id);
$smarty->assign('sAccion',$LDAction);
$smarty->assign('sFecha',$LDDate);

$smarty->assign('LDArchivos',$LDAdjuntos);

if ($minimal) {
  $smarty->assign('sFileBrowser', $archivos);
  $smarty->assign('sListaArchtext','');
 } else {

  $smarty->assign('sListaArchtext','<input name="listaArchivos" type="text" size="35" readonly>');
  $smarty->assign('sFileBrowser',"<a href=\"javascript:popWin()\"><img ".createComIcon($root_path,'b-write_addr.gif','0')."></a>" . $archivos);
 }





$smarty->assign('img', createLDImgSrc($root_path,'ok_small.gif','0'));
$smarty->assign('img2', createLDImgSrc($root_path,'add_sm.gif','0'));


$smarty->assign('sToolbarTitle',$LDHC);

$smarty->assign('title',$LDsoap);

$lang_tables =array('person.php', 'interconsulta.php', 'history.php');
include($root_path.'include/inc_load_lang_tables.php');

ob_start();
if (!$minimal) {
  require('../registration_admission/gui_bridge/default/gui_patient_his_options.php');
 }

$tab_bot_line='#66ee66';
require('./gui_bridge/default/gui_tabs_patreg.php');


$sTemp = ob_get_contents();
ob_end_clean();

$smarty->assign('sRegOptions',$sTemp);
$smarty->assign('gui',$gui);
$smarty->assign('root_path',$root_path);
$smarty->assign('append', URL_APPEND);

if ((!$update) || ($auditor)) {
$smarty->assign('pbSubmit','<input type="image" '.createLDImgSrc($root_path,'savedisc.gif','0').'  alt="'.$LDSaveData.'" align="absmiddle">');
$smarty->assign('pbReset','<a href="javascript:document.regform.reset()"><img '.createLDImgSrc($root_path,'reset.gif','0').' alt="'.$LDResetData.'" align="absmiddle"></a>');
 } else {
  $smarty->assign('pbSubmit','');
  $smarty->assign('pbReset','');
 }

$smarty->assign('sMainBlockIncludeFile','historia_clinica/historia_clinica-soap.tpl');

if ($minimal) {
$smarty->assign('hideTabs',true);
$smarty->display('historia_clinica/historia_clinica-soap.tpl');
 } else {
$smarty->display('common/mainframe.tpl');
 }
?>
